#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int dis[N];
int a, n;
int bfs(int x)
{
	queue<int>q;
	memset(dis, -1, sizeof(dis));
	q.push(a);
	dis[a] = 0;
	while (q.size()) {
		int t = q.front();
		q.pop();
		if (t == x) {
			return dis[x];
		}
		if (dis[t - 1] == -1) {
			dis[t - 1] = dis[t] + 1;
			q.push(t - 1);
		}
		if (dis[t * 3] == -1) {
			dis[t * 3] = dis[t] + 1;
			q.push(t * 3);
		}
		if (dis[t + 1] == -1) {
			dis[t + 1] = dis[t] + 1;
			q.push(t + 1);
		}
		if (dis[t * 2] == -1) {
			dis[t * 2] = dis[t] + 1;
			q.push(t * 2);
		}	
	}
}	
int main()
{
	cin >> a >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	for (int i = 1; i <= n; i++) {
		int ans = bfs(arr[i]);
		cout << ans << " ";
	}
	return 0;
}